﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Blogs
{
    public partial class BlogEdit : System.Web.UI.Page
    {
        private string id = "";
        private string otheruserid = "";
        private Blog binfo = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] + "";
            otheruserid = Request.QueryString["otheruserid"] + "";
            if (!Page.IsPostBack)
            {
                if (otheruserid == "")
                {
                    //判断该博客是否为该用户的
                    if (!CheckBlog())
                    {
                        Tools.AlertAndRedirect("该博客不允许您进行编辑", "BlogList.aspx");
                        return;
                    }
                }
                BindBlogType();
                
                LoadContent();
            }
        }

        #region 加载博客类型
        private void BindBlogType()
        {
            BlogHelper bhelper = new BlogHelper();
            binfo = bhelper.GetBlog(id);

            BlogTypeHelper bthelper = new BlogTypeHelper();

            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "BlogBigType", binfo.BlogBigType);
            List<BlogType> list = bthelper.QueryBlogTypeBySearch(c, null);
            rblBlogType.DataSource = list;
            rblBlogType.DataTextField = "BlogTypeName";
            rblBlogType.DataValueField = "ID";
            rblBlogType.DataBind();

            if (list != null && list.Count > 0)
            {
                rblBlogType.Items[0].Selected = true;
            }
        }
        #endregion

        #region 加载内容
        private void LoadContent()
        {
            if (binfo == null)
            {
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(id);
            }
            
            litBlogBigType.Text = binfo.BlogBigType;
            rblBlogType.SelectedValue = binfo.BlogTypeID;
            txtTitle.Text = binfo.Title;
            txtKeyWord.Text = binfo.KeyWord;
            int moneytype = binfo.MoneyType;
            if (moneytype == 0)
            {
                litMoneyType.Text = "免费";
            }
            else if (moneytype == 1)
            {
                litMoneyType.Text = "收费";
            }
            litCostMoney.Text = binfo.CostMoney.ToString();
            txtContent.Value = binfo.BlogContent;

            string companyid = binfo.CompanyID;
            if (string.IsNullOrEmpty(companyid)||companyid=="0")//小组为空 可以重新设置小组
            {
                BindCompany();//加载我加入的小组
            }
            else//小组不为空，不能进行修改
            {
                licompany.Visible = false;
            }

            int otheredit = binfo.OtherEdit;
            if (string.IsNullOrEmpty(otheruserid))
            {
                liotheredit.Visible = true;
                if (otheredit == 0)
                {
                    cbOtherEdit.Checked = false;
                }
                else if (otheredit == 1)
                {
                    cbOtherEdit.Checked = true;
                }
            }
            else
            {
                liotheredit.Visible = false;
                liblogbigtype.Visible = false;
                liblogtype.Visible = false;
                licompany.Visible = false;
            }
        }
        #endregion

        #region 保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //当前登录用户 
            Users uinfo = UserBase.GetCurrentUser();

            string blogtype = rblBlogType.SelectedValue;
            string blogtypename = rblBlogType.SelectedItem.Text;
            string title = txtTitle.Text.Trim();
            string keyword = txtKeyWord.Text.Trim();
            keyword = keyword.Replace("，", " ");
            keyword = keyword.Replace(",", " ");
            string blogcontent = txtContent.Value.Trim();

            int otheredit = 0;
            if (cbOtherEdit.Checked)
            {
                otheredit = 1;
            }

            Blog binfo = new Blog();
            BlogHelper bhelper = new BlogHelper();

            binfo = bhelper.GetBlog(id);

            //判断是自己编辑还是其他人编辑
            if (string.IsNullOrEmpty(otheruserid) || otheruserid == binfo.UsersID)
            {
                //得到该博客中第一张图片的地址
                string firstpicpath = bhelper.GetFirstPicPath(blogcontent);
                if (firstpicpath != "")
                {
                    binfo.HasPic = 1;
                    if (firstpicpath.Contains("http"))
                    {
                        binfo.FirstPicPath = firstpicpath;
                    }
                    else
                    {
                        string serverpath = UserBase.GetServerPath();
                        binfo.FirstPicPath = serverpath + firstpicpath;
                    }
                }

                binfo.BlogTypeID = blogtype;
                binfo.BlogTypeName = blogtypename;
                binfo.Title = title;
                binfo.KeyWord = keyword;
                binfo.OtherEdit = otheredit;
                string companyid = "0";
                if (licompany.Visible)
                {
                    companyid = rblCompany.SelectedValue;
                    binfo.CompanyID = companyid;
                }

                binfo.BlogContent = blogcontent;

                bhelper.EditBlog(binfo);
                Tools.AlertAndRedirect("修改成功！", "BlogList.aspx");
            }
            else//他人进行编辑 插入到博客编辑记录表中 需要等待博主审批通过之后才能采纳
            {
                BlogOtherEdit boeinfo = new BlogOtherEdit();
                BlogOtherEditHelper boehelper = new BlogOtherEditHelper();
                boeinfo.UsersID = otheruserid;
                boeinfo.NickName = uinfo.NickName;
                boeinfo.BlogUsersID = binfo.UsersID;
                boeinfo.Title = title;
                boeinfo.KeyWord = keyword;
                boeinfo.BlogContent = blogcontent;
                boeinfo.BlogID = id;
                boeinfo.Status = 0;
                boehelper.AddBlogOtherEdit(boeinfo);

                //给博主发送消息
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                minfo.FromUsersID = uinfo.ID;
                minfo.FromNickName = uinfo.NickName;
                minfo.ToUsersID = boeinfo.BlogUsersID;
                minfo.MessageContent = uinfo.NickName + "编辑了您的创作：" + binfo.Title + "，<a href=\"../Blogs/AuditBlogOtherEdit.aspx?id=" + id + "\">点击这里查看</a>！";
                minfo.Status = 0;
                minfo.Flag = 0;
                mhelper.AddMessage(minfo);

                Tools.AlertAndRedirect("提交成功，经过采纳之后才能显示，感谢！", "../b/"+id+".aspx");
            }
        }
        #endregion

        #region 判断该博客是否为该用户的
        private bool CheckBlog()
        {
            Users uinfo=UserBase.GetCurrentUser();
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "ID", id);
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            int count = bhelper.QueryBlogCountBySearch(c);
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 加载我加入的小组
        private void BindCompany()
        {
            Users uinfo = UserBase.GetCurrentUser();
            string userid = uinfo.ID;

            UserCompanyHelper uchelper = new UserCompanyHelper();
            DataSet dsCompany = uchelper.GetUserAddCompany(userid, 999);

            if (dsCompany != null && dsCompany.Tables[0].Rows.Count > 0)
            {
                licompany.Visible = true;

                rblCompany.DataSource = dsCompany;
                rblCompany.DataTextField = "CompanyName";
                rblCompany.DataValueField = "id";
                rblCompany.DataBind();

                ListItem li = new ListItem();
                li.Value = "0";
                li.Text = "不分配小组";
                li.Selected = true;
                rblCompany.Items.Insert(0, li);
            }
            else//加入小组为空
            {
                licompany.Visible = false;
            }
        }
        #endregion

        #region 返回
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("BlogList.aspx");
        }
        #endregion
    }
}